package com.dmz.oauth2.utils;

import java.util.Random;
import java.util.UUID;

import org.springframework.util.StringUtils;

public class RandomUtils {
    public RandomUtils() {
    }

    public static String getVerCode(int n) {
        String val = "";
        Random random = new Random();

        for(int i = 0; i < n; ++i) {
            val = val + String.valueOf(random.nextInt(10));
        }

        return val;
    }

    public static String getVerCode() {
        return getVerCode(4);
    }

    public static String getCode(int len) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();

        for(int i = 0; i < len; ++i) {
            boolean bool = random.nextInt(2) % 2 == 0;
            if (bool) {
                val.append(getNumCode(1));
            } else {
                val.append(getStrCode(1));
            }
        }

        return val.toString();
    }

    public static String getStrCode(int len) {
        Random random = new Random();
        StringBuilder val = new StringBuilder();

        for(int i = 0; i < len; ++i) {
            int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
            val.append((char)(nextInt + random.nextInt(26)));
        }

        return val.toString();
    }

    public static String getStrUpperCaseCode(int len) {
        String strCode = getStrCode(len);
        return strCode.toUpperCase();
    }

    public static String getNumCode(int len) {
        Random random = new Random();
        StringBuilder val = new StringBuilder();

        for(int i = 0; i < len; ++i) {
            val.append(random.nextInt(10));
        }

        return val.toString();
    }

    public static String getTraceId(String busKey) {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isEmpty(new CharSequence[]{busKey})) {
            builder.append(busKey).append("_");
        }

        builder.append(UUID.randomUUID().toString());
        return builder.toString();
    }

    public static String getTraceId() {
        return UUID.randomUUID().toString();
    }
}